package com.scu.controller;

import com.scu.pojo.FriendlyLink;
import com.scu.service.FriendlyLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class FriendlyLinkController {
    @Autowired
    private FriendlyLinkService friendlyLinkService;

    @RequestMapping("getFriendlyLink")
    @ResponseBody
    public HashMap getFriendlyLink(){
        List<FriendlyLink> friendlyLinkList = friendlyLinkService.getFriendlyLink();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", friendlyLinkList);
        return map;
    }

    @RequestMapping("addFriendlyLink")
    @ResponseBody
    public HashMap addFriendlyLink(HttpServletRequest request){
        String link_name = request.getParameter("link_name");
        String link_content = request.getParameter("link_content");
//        System.out.println(link_name+","+link_content);
        int result = friendlyLinkService.addFriendlyLink(link_name, link_content);
        HashMap<String,Object> map = new HashMap<>();

        if(result == 1){
            map.put("code", 200);
            map.put("msg", "");
        }else {
            map.put("code", 500);
            map.put("msg", "插入失败");
        }

        return map;
    }

    @RequestMapping("updateFriendlyLink")
    @ResponseBody
    public HashMap updateFriendlyLink(HttpServletRequest request) {
        int link_id = Integer.parseInt(request.getParameter("link_id"));
        String link_name = request.getParameter("link_name");
        String link_content = request.getParameter("link_content");
        FriendlyLink fk = new FriendlyLink();
        fk.setLink_id(link_id);
        fk.setLink_name(link_name);
        fk.setLink_content(link_content);
        int result = friendlyLinkService.updateFriendlyLink(fk);

        HashMap<String,Object> map = new HashMap<>();
        if(result == 1){
            map.put("code", 200);
            map.put("msg", "");
        }else {
            map.put("code", 500);
            map.put("msg", "请求失败");
        }
        return map;
    }

    @RequestMapping("deleteFriendlyLink")
    @ResponseBody
    public HashMap deleteFriendlyLink(HttpServletRequest request){
        int link_id = Integer.parseInt(request.getParameter("link_id"));
        int result = friendlyLinkService.deleteFriendlyLink(link_id);
        HashMap<String,Object> map = new HashMap<>();

        if(result == 1){
            map.put("code", 200);
            map.put("msg", "");
        } else {
            map.put("code", 500);
            map.put("msg", "请求失败");
        }
        return map;
    }
}
